from classification_mode_l import TextCnn, TextBilstm, TextLstm, TextBilstmAttention, TextRcnn,Svm,NaiveBayes
from word2vec_l import get_data_machine,get_data_neural, remove_stopword, use_word2vec
import numpy as np
import jieba
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.optimizers import Adam, SGD

# 定义优化器
opts = dict({
    'adam': Adam(),
    'sgd': SGD(),

})


# 处理训练机器学习模型的数据
def machine_data_processing(data):
    stop_word_list = ['very', 'ourselves', 'am', 'doesn', 'through', 'me', 'against', 'up', 'just', 'her', 'ours',
                      'couldn', 'because', 'is', 'isn', 'it', 'only', 'in', 'such', 'too', 'mustn', 'under', 'their',
                      'if', 'to', 'my', 'himself', 'after', 'why', 'while', 'can', 'each', 'itself', 'his', 'all',
                      'once',
                      'herself', 'more', 'our', 'they', 'hasn', 'on', 'ma', 'them', 'its', 'where', 'did', 'll', 'you',
                      'didn', 'nor', 'as', 'now', 'before', 'those', 'yours', 'from', 'who', 'was', 'm', 'been', 'will',
                      'into', 'same', 'how', 'some', 'of', 'out', 'with', 's', 'being', 't', 'mightn', 'she', 'again',
                      'be',
                      'by', 'shan', 'have', 'yourselves', 'needn', 'and', 'are', 'o', 'these', 'further', 'most',
                      'yourself', ',', '，', '.', '。', '!', '~', '`', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-',
                      '+',
                      '=', '-', 'having', 'are', 'here', 'he', 'were', 'but', 'this', 'myself', 'own', 'we', 'so', 'i',
                      'does', 'both',
                      'when', 'between', 'd', 'had', 'the', 'y', 'has', 'down', 'off', 'than', 'haven', 'whom',
                      'wouldn',
                      'should', 've', 'over', 'themselves', 'few', 'then', 'hadn', 'what', 'until', 'won', 'no',
                      'about',
                      'any', 'that', 'for', 'shouldn', 'don', 'do', 'there', 'doing', 'an', 'or', 'ain', 'hers', 'wasn',
                      'weren', 'above', 'a', 'at', 'your', 'theirs', 'below', 'other', 'not', 're', 'him', 'during',
                      'which', '.........', ' ', "'", ':', '"', '<', '/', 'br', '>', '?', '...', '\x85', '1', '2', '3',
                      '4',
                      '5', '6', '7', '8', '9', '10', ';', '\x97']
    result_all = []
    for i in range(len(data)):
        result = [k for k in jieba.lcut(data[i], cut_all=False) if k not in stop_word_list]
        result_all.append(" ".join(result))
    return result_all

# 定义在训练期间将使用的回调函数
def create_callbacks(opt, name):
    callbacks = [
        EarlyStopping(monitor='val_acc', patience=10, verbose=2),
        ModelCheckpoint('best_model_' + name + opt + '.h5', monitor='val_acc', save_best_only=True, verbose=0)
    ]
    return callbacks


# 获取神经网络训练数据
def get_train_data_neural():
    x_train, train_label, x_test, test_label = get_data_neural()
    x_train = remove_stopword(x_train)
    x_test = remove_stopword(x_test)
    x_train = np.array(use_word2vec(x_train))
    x_test = np.array(use_word2vec(x_test))
    test_sample_index = -1 * int(0.2 * float(len(train_label)))
    x_train_data, x_valid_data = x_train[:test_sample_index], x_train[test_sample_index:]
    y_train_data, y_valid_data = train_label[:test_sample_index], train_label[test_sample_index:]
    return x_train_data, x_valid_data, y_train_data, y_valid_data, x_test, test_label


# 获取机器学习训练数据
def get_train_data_machine():
    x_train, train_label, x_test, test_label = get_data_machine()
    x_train = machine_data_processing(x_train)
    x_test = machine_data_processing(x_test)
    return x_train, train_label, x_test, test_label


# 训练模型神经网络
def train_model_neural(model_name, x_train_data, y_train_data, x_valid_data, y_valid_data):
    result = {'adam': '', 'sgd': ''}
    if model_name == 'cnn':
        for opt in opts:
            result_list = []
            model = TextCnn(maxlen=200, dims=100, class_num=2).get_model()
            callbacks = create_callbacks(opt, model_name)
            model.compile(loss='categorical_crossentropy',
                          optimizer=opts[opt],
                          metrics=['acc'])
            history = model.fit(x_train_data, y_train_data, batch_size=128, epochs=30,
                                validation_data=(x_valid_data, y_valid_data), validation_batch_size=128,
                                callbacks=callbacks)
            best_epoch = np.argmax(history.history['val_acc'])
            best_acc = history.history['val_acc'][best_epoch]
            result_list.append([best_epoch, best_acc])
            result[opt] = result_list
    if model_name == 'lstm':
        for opt in opts:
            result_list = []
            model = TextLstm(maxlen=200, dims=100, class_num=2).get_model()
            callbacks = create_callbacks(opt, model_name)
            model.compile(loss='categorical_crossentropy',
                          optimizer=opts[opt],
                          metrics=['acc'])
            history = model.fit(x_train_data, y_train_data, batch_size=128, epochs=30,
                                validation_data=(x_valid_data, y_valid_data), validation_batch_size=128,
                                callbacks=callbacks)
            best_epoch = np.argmax(history.history['val_acc'])
            best_acc = history.history['val_acc'][best_epoch]
            result_list.append([best_epoch, best_acc])
            result[opt] = result_list
    if model_name == 'Bilstm':
        for opt in opts:
            result_list = []
            model = TextBilstm(maxlen=200, dims=100, class_num=2).get_model()
            callbacks = create_callbacks(opt, model_name)
            model.compile(loss='categorical_crossentropy',
                          optimizer=opts[opt],
                          metrics=['acc'])
            history = model.fit(x_train_data, y_train_data, batch_size=128, epochs=30,
                                validation_data=(x_valid_data, y_valid_data), validation_batch_size=128,
                                callbacks=callbacks)
            best_epoch = np.argmax(history.history['val_acc'])
            best_acc = history.history['val_acc'][best_epoch]
            result_list.append([best_epoch, best_acc])
            result[opt] = result_list
    if model_name == 'Bilstm_attention':
        for opt in opts:
            result_list = []
            model = TextBilstmAttention(maxlen=200, dims=100, class_num=2).get_model()
            callbacks = create_callbacks(opt, model_name)
            model.compile(loss='categorical_crossentropy',
                          optimizer=opts[opt],
                          metrics=['acc'])
            history = model.fit(x_train_data, y_train_data, batch_size=128, epochs=30,
                                validation_data=(x_valid_data, y_valid_data), validation_batch_size=128,
                                callbacks=callbacks)
            best_epoch = np.argmax(history.history['val_acc'])
            best_acc = history.history['val_acc'][best_epoch]
            result_list.append([best_epoch, best_acc])
            result[opt] = result_list
    if model_name == 'Rcnn':
        for opt in opts:
            result_list = []
            model = TextRcnn(maxlen=200, dims=100, class_num=2).get_model()
            callbacks = create_callbacks(opt, model_name)
            model.compile(loss='categorical_crossentropy',
                          optimizer=opts[opt],
                          metrics=['acc'])
            history = model.fit(x_train_data, y_train_data, batch_size=128, epochs=30,
                                validation_data=(x_valid_data, y_valid_data), validation_batch_size=128,
                                callbacks=callbacks)
            best_epoch = np.argmax(history.history['val_acc'])
            best_acc = history.history['val_acc'][best_epoch]
            result_list.append([best_epoch, best_acc])
            result[opt] = result_list
    return result


# 训练机器学习模型
def train_model_machine(model_name,x_train,y_train,x_test,y_test):
    score =0
    if model_name=="svm":
        model = Svm()
        model.fit(x_train,y_train)
        score =model.score(x_test, y_test)

    if model_name=="nbs":
        model = NaiveBayes()
        model.fit(x_train,y_train)
        score = model.score(x_test,y_test)
    return score


if __name__ == '__main__':
    x_train, train_label, x_test, test_label = get_train_data_machine()
    # print(x_train[0])
    train_model_machine("svm", x_train, train_label, x_test, test_label)
    # x_train_data, x_valid_data, y_train_data, y_valid_data, x_test, test_label = get_train_data_neural()
    # result = train_model_neural('lstm', x_train_data, y_train_data, x_valid_data, y_valid_data)
    # print(result)
